/**
 * 
 */
package edu.cmu.cs.webapp.ioms.ui.providers.order;

import java.text.DateFormat;
import java.text.NumberFormat;

import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

import edu.cmu.cs.webapp.ioms.models.Ingredient;
import edu.cmu.cs.webapp.ioms.utils.IOMSImageHelper;
import edu.cmu.cs.webapp.ioms.utils.IOMSImages;

/**
 * @author Christian
 * 
 */
public class InventoryTableViewerLabelProvider extends LabelProvider implements
		ITableLabelProvider, ITableColorProvider {
	// table column index
	public static final int ID = 0;
	public static final int NAME = 1;
	public static final int DEMAND = 2;
	public static final int STOCK = 3;
	public static final int SAFETY = 4;
	public static final int PRICE = 5;
	public static final int LAST_UPDATE = 6;

	private double safetyLevel = 1;

	public InventoryTableViewerLabelProvider(double safetyLevel) {
		this.safetyLevel = safetyLevel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
	 * .Object, int)
	 */
	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		if (element instanceof Ingredient) {
			Ingredient ing = (Ingredient) element;
			Image img = null;
			if (ing.getDemand() > ing.getInventory()) {
				img = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_INVENTORY_ALERT);
			} else if (ing.getDemand() <= ing.getInventory()
					&& ing.getDemand() >= (1 - safetyLevel)
							* ing.getInventory()) {
				img = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_INVENTORY_WARNNING);
			} else if (ing.getDemand() < (1 - safetyLevel) * ing.getInventory()) {
				img = IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_INVENTORY);
			}

			switch (columnIndex) {
			case ID:
				return img;
			}
		}

		return null;
	}

	public void setSafetyLevel(double safetyLevel) {
		this.safetyLevel = safetyLevel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang
	 * .Object, int)
	 */
	@Override
	public String getColumnText(Object element, int columnIndex) {
		if (element instanceof Ingredient) {
			Ingredient ing = (Ingredient) element;
			switch (columnIndex) {
			case ID:
				return "#" + ing.getIng_id();
			case NAME:
				return ing.getName();
			case DEMAND:
				return NumberFormat.getInstance().format(ing.getDemand());
			case STOCK:
				return NumberFormat.getInstance().format(ing.getInventory());
			case PRICE:
				return NumberFormat.getCurrencyInstance().format(
						ing.getUnit_cost());
			case SAFETY:
				return NumberFormat.getInstance().format(
						safetyLevel * ing.getInventory());
			case LAST_UPDATE:
				return ing.getLastUpdate() == null ? "" : DateFormat
						.getDateInstance().format(ing.getLastUpdate());
			default:
				return "";
			}
		}

		return "";

	}

	@Override
	public Color getBackground(Object element, int columnIndex) {
		return null;
	}

	@Override
	public Color getForeground(Object element, int columnIndex) {
		if (element instanceof Ingredient) {
			if (columnIndex == STOCK) {
				return Display.getDefault().getSystemColor(SWT.COLOR_BLUE);
			}
		}
		return null;
	}
}
